import { MessageBox } from "element-ui";

export const $alert = function({
  type,
  title,
  message,
  callback,
  confirmButtonText
}) {
  let icon = "el-icon-" + type;
  let content =
    "<i class='" +
    icon +
    " msg-icon'></i>" +
    "<span class='msg-title'>" +
    title +
    "</span>";
  if (message) {
    content += `<div class='msg-content'>${message}</div>`;
  }
  MessageBox.alert(content, "", {
    confirmButtonText: confirmButtonText ? confirmButtonText : "确定",
    dangerouslyUseHTMLString: true,
    callback: callback ? callback : () => {}
  });
};

export const $confirm = function({
  type,
  title,
  message,
  confirm,
  cancel,
  confirmButtonText,
  cancelButtonText,
  showCancelButton = true,
  showConfirmButton = true,
  closeOnClickModal = false,
  distinguishCancelAndClose = false,
  html
}) {
  // type success / info / warning / error
  let content = `<i class='el-icon-${type} msg-icon'></i><span class='msg-title'>${title}</span>`;
  if (html) {
    content += html;
  } else if (message) {
    content += `<div class='msg-content'>${message}</div>`;
  } else {
    content += `<div class='msg-content'></div>`;
  }

  MessageBox.confirm(content, "", {
    confirmButtonText: confirmButtonText ? confirmButtonText : "确定",
    cancelButtonText: cancelButtonText ? cancelButtonText : "关闭",
    dangerouslyUseHTMLString: true,
    showCancelButton,
    showConfirmButton,
    closeOnClickModal,
    distinguishCancelAndClose
  })
    .then(() => {
      if (confirm) {
        confirm && confirm();
      }
    })
    .catch(action => {
      if (action === "close") {
        close && close();
      } else {
        cancel && cancel();
      }
    });
};
